<template>
	<navBarBase navTitle="首页" @click-left="showProjectPicker" backgroundColor="#fff">
		<!-- backgroundColor="#108EE9 -->
		<view slot="left">

			<view class="project-selector">
				{{currentProject.name || ''}}
			</view>
			<lb-picker @cancel="handleCancel" @confirm="handleConfirm" ref="picker" mode="selector" :value="currentProject.id"
			 :list="projectList.map(x=>x.name)"></lb-picker>
		</view>

		<!-- <view slot="right">right</view> -->
	</navBarBase>
</template>

<script>
	import navBarBase from '../../myComponents/custom-nav-bar/nav-bar-base.vue'
	import LbPicker from '../../components/lb-picker/index.vue'
	export default {
		components: {
			navBarBase,
			LbPicker
		},
		data() {
			return {
				projectList: [],
				currentProject: {},
			}
		},
		created() {
			this.refresh()
		},
		onShow() {
			
		},
		methods: {
			handleCancel(e) {
				uni.showTabBar({
					animation: true
				})
				// console.log('cancel::', e)
			},
			refresh() {
				this.projectList = uni.getStorageSync('projList') || []
				this.currentProject = this.projectList.find(x => x.id == uni.getStorageSync("projId"))		
			},
			showProjectPicker() {
				uni.hideTabBar({
					animation: true
				})
				this.$refs.picker.show()
			},
			handleConfirm(e) {
				uni.showTabBar({
					animation: true
				})
			
				let index = e.index
					// console.log(this.projectList[index])
				if (this.currentProject &&  this.projectList[index].id == this.currentProject.id) {
					return
				}
				let that =this
				uni.showModal({
					title: '提示',
					content: '您确定要切换到' + e.value + '吗?',
					success: async (res) => {
						if (res.confirm) {
							if (e) {
								this.currentProject = this.projectList[index]

								let userRes = await this.$http.post('common/user/info', {
									projId: this.currentProject.id
								})

								if (userRes.code === 10000 || userRes.code === 50014 || userRes.code === 50015) {
								
									//存在用户才设置全局projId
									uni.setStorageSync('projId', userRes.data.projId)
									uni.setStorageSync('userInfo', userRes.data)
									uni.showToast({
										title: "切换成功"
									})
									console.log('xx')
									that.$emit('projChange')
									
									if (userRes.code === 50014) {
										uni.showModal({
											title: '提示',
											content: '当前小区注册信息处于待审核状态,部分功能将不可用,请尽快前往小区物业进行审核',
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													console.log('点击确定')
												}
											}
										});
									}
									if (userRes.code === 50015) {
										uni.showModal({
											title: '提示',
											content: '当前小区注册信息未通过审核,请重新完善个人信息',
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													uni.navigateTo({
														url: '../userinfo/userinfo?projId=' + this.currentProject.id
													})
												}
											}
										});
									}

								}

								if (userRes.code === 50013) {
									uni.showModal({
										title: '提示',
										content: '当前小区用户不存在,请前往注册',
										showCancel: false,
										success: (res) => {
											if (res.confirm) {
												uni.navigateTo({
													url: '../register/register?projId=' + this.currentProject.id
												})
											}
										}
									});
								}


							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});


			},
			// 查询用户信息
			getUserInfo() {
				this.$http.post('common/user/info', {
					projId: this.currentProject.id
				}).then(res => {
					console.log(res, 123)
					if (res.code === 10000 || res.code === 50014 || res.code === 50015) {
						this.userInfo = res.data
						uni.setStorageSync('projId', res.data.projId)
						uni.setStorageSync('userInfo', res.data)

						if (res.code === 50014) {
							uni.showModal({
								title: '提示',
								content: '当前小区注册信息处于待审核状态,部分功能将不可用,请尽快前往小区物业进行审核',
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										console.log('点击确定')
									}
								}
							});
						}
						if (res.code === 50015) {
							uni.showModal({
								title: '提示',
								content: '当前小区注册信息未通过审核,请重新完善个人信息',
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										uni.navigateTo({
											url: '../userinfo/userinfo'
										})
									}
								}
							});
						}

					}

					if (res.code === 50013) {
						uni.showModal({
							title: '提示',
							content: '当前小区用户不存在,请前往注册',
							showCancel: false,
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: '../register/register?projId=' + this.currentProject.id
									})
								}
							}
						});
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.project-selector {
		color: #4F4F4F;
		font-size: 32rpx;
	}

	.project-selector::after {
		content: "";
		display: inline-block;
		width: 0;
		height: 0;
		border: 10rpx solid;
		border-color: #4F4F4F transparent transparent;
		transform: translate(15rpx, 5rpx);
	}
</style>
